package com.etc.controller;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

import com.etc.entity.Counter;
import com.etc.service.CounterService;

public class SerializeOperation {


	// 序列化数据
	// 包装要发送的语句
	public static <T> byte[] packing(T t) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		//利用DataOutputStream包装，方便写入数据
		DataOutputStream dos = new DataOutputStream(baos); 
		byte[] buf = null;
		try {
			// 将文件输出流DataOutputStream保证起来并一次写入数据
			ObjectOutputStream oos = new ObjectOutputStream(dos);
			oos.writeObject(t); // 用对象数组存放进组，取出来的也是对象数组
			buf = baos.toByteArray();
			// 关闭流，防止占用资源
			
			oos.close();
			dos.close();
			baos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return buf;
	}

	// 反序列化解析
	public static Counter analysis(byte[] buf) {
		// 将数组中的数据还原为流，并读取数据
		DataInputStream dis = new DataInputStream(new ByteArrayInputStream(buf));
		Counter user = new Counter();
		try {

			ObjectInputStream ois = new ObjectInputStream(dis);
			user = (Counter) ois.readObject();// 反序列化
			ois.close();
			dis.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// 用来将对象序列化后存进文件中
	//是个泛型方法，自动根据对象的不同，采用对应的返回值类型，，
	public static <T> void saveToFile(T t, String fileName) {
		FileOutputStream fos = null; // 文件输入流
		File file = new File("D:/"+fileName);
		try {
			// 如果FileOutputStream构造器第二个参数为false(默认值)，则每次都会将原文件删除
			fos = new FileOutputStream(file);
			// 将文件输出流DataOutputStream保证起来并一次写入数据
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(t); // 用对象数组存放进组，取出来的也是对象数组
			// 关闭流，防止占用资源
			oos.close();
			fos.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 从文件中取出，然后反序列化成对象
     //是个泛型方法，自动根据对象的不同，采用对应的返回值类型，，
	public static <T> T loadFromFile(String fileName) {
		FileInputStream fis = null; // 文件输出流
		File file = new File("D:/" + fileName);
		T t = null;
		try {
			fis = new FileInputStream(file);
			ObjectInputStream ois = new ObjectInputStream(fis);
			t = (T) ois.readObject();// 反序列化
			fis.close();
			ois.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return t;
	}
	
	/** 
     * 获得指定文件的byte数组 
     */  
    public static byte[] getBytes(String fileName){  
        byte[] buffer = null;  
        File file = new File("D:/" + fileName);
        try {  
            FileInputStream fis = new FileInputStream(file);  
            ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);  
            byte[] b = new byte[1000];  
            int n;  
            while ((n = fis.read(b)) != -1) {  
                bos.write(b, 0, n);  
            }  
            fis.close();  
            bos.close();  
            buffer = bos.toByteArray();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return buffer;  
    }
    
    /** 
     * 根据byte数组，生成文件 
     */  
    public static void getFile(byte[] bfile) {  
        BufferedOutputStream bos = null;  
        FileOutputStream fos = null;  
        File file = null;  
        String filePath = "D:\\";
        String fileName = "text2.dat";
        try {  
            File dir = new File(filePath);  
            if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在  
                dir.mkdirs();  
            }  
            file = new File(filePath+"\\"+fileName);  
            fos = new FileOutputStream(file);  
            bos = new BufferedOutputStream(fos);  
            bos.write(bfile);  
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            if (fos != null) {  
                try {  
                    fos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
        }  
    }
	
	
	public static void main(String[] args) {
		CounterService counterSer = new CounterService();
		ArrayList<Counter> counters = counterSer.findAllCounter();
		// byte[] buf = packing(counters.get(0));
		// Counter analysis = analysis(buf);
		// System.out.println(analysis.toString());
//		saveToFile(counters.get(0));
//		Counter loadFromFile = loadFromFile();
//		System.out.println(loadFromFile.toString());
//		byte[] bytes = getBytes();
//		getFile(bytes);
	}
}
